package com.nothing.Dao;

import com.nothing.Model.Banner;
import com.nothing.Model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

/**
 * Created by Administrator on 2018/3/28.
 */
public class BannerDaoImpl implements IBannerDao{
    private SqlSessionFactory sessionFactory;
    private SqlSession session;
    public BannerDaoImpl() {
        String resource = "conf.xml";
        try {
            Reader reader = Resources.getResourceAsReader(resource);
            sessionFactory = new SqlSessionFactoryBuilder().build(reader);
            session = sessionFactory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<Banner> findBanners() {
        String statement = "bannerMapper.findBanners";
        List<Banner> banners = session.selectList(statement,"");
        return banners;
    }
    public void addBanner(Banner banner){
        String statement = "bannerMapper.addBanner";
        session.insert(statement, banner);
        session.commit();  //一定要记得commit
    }
}
